<?php

namespace app\common\model\subject;

use think\Model;


class Teacher extends Model
{
    // 指定数据表
    protected $name = 'teacher';

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义创建时间字段
    protected $createTime = 'createtime';

    // 定义更新时间字段 => false 禁用
    protected $updateTime = false;

    // 追加数据表不存在的字段
    protected $append = [
        'avatar_cdn',
        'subject_count',
        'follow_count'
    ];

    public function getAvatarCdnAttr($value, $data)
    {
        $avatar = !empty($data['avatar']) ? $data['avatar'] : '';

        $domain = request()->domain();

        if (!$avatar || is_file('.' . $avatar)) {
            return $domain . '/assets/img/avatar.png';
        }

        return $domain . $avatar;
    }

    public function getSubjectCountAttr($value, $data)
    {
        $teacherid = $data['id'] ?? 0;

        $subjectCount = model('subject.Subject')->where(['teacherid' => $teacherid])->count();

        return $subjectCount;
    }

    public function getFollowCountAttr($value, $data)
    {
        $teacherid = $data['id'] ?? 0;

        $FollowCount = model('subject.Follow')->where(['teacherid' => $teacherid])->count();

        return $FollowCount;
    }
}
